package com.cy.pj.sys.controller;

import com.cy.pj.common.vo.JsonResult;
import com.cy.pj.sys.pojo.SysUser;
import com.cy.pj.sys.service.SysUserService;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.swing.*;
import java.util.Map;

/**
 * @Author YuBaoFu
 * @create 2025/06/04
 * @description
 */
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping("/doFindPageObjects")
    public JsonResult doFindPageObjects(String username, Integer pageCurrent){
        return new JsonResult(sysUserService.findPageObjects(username,pageCurrent));
    }

    @RequestMapping("doValidById")
    public JsonResult doValidById(Integer id,Integer valid){
        sysUserService.validById(id,valid);
        return new JsonResult("update ok");
    }

    @RequestMapping("doSaveObject")
    public JsonResult doSaveObject(SysUser sysUser,Integer[] roleIds){
        sysUserService.saveObject(sysUser,roleIds);
        return new JsonResult("save OK!~");
    }

    @RequestMapping("doFindObjectById")
    public JsonResult doFindObjectById(Integer id){
        Map<String,Object> map=sysUserService.findObjectById(id);
        return new JsonResult(map);
    }

    @RequestMapping("doUpdateObject")
    public JsonResult doUpdateObject(SysUser sysUser,Integer[] roleIds){
        sysUserService.updateObject(sysUser,roleIds);
        return new JsonResult("update ok");
    }

    @RequestMapping("doLogin")
    public JsonResult doLogin(Boolean isRememberMe,String username,String password){
        /*获取主体对象*/
        Subject subject = SecurityUtils.getSubject();
        /*封装token：username、password*/
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
     /*   if (isRememberMe){
            token.setRememberMe(true);
        }*/
        token.setRememberMe(isRememberMe);
        subject.login(token);
        //1)token会传给shiro的SecurityManager
        //2)SecurityManager将token传递给认证管理器
        //3)认证管理器会将token传递给realm
        return new JsonResult("login OK!~");
    }

}
